<template>
  <div class="toolbar">
    <div class="arrow up"
         @click="scrollUp"
         v-if="totalFloors > 5"></div>
    <div class="btn-group">
      <div class="scroll-line"
           :style="`margin-top: ${marginTop}px;`"></div>
      <div class="floor-btn"
           :class="{ active: activeFloor === totalFloors - i + 1 }"
           @click="floorChange(totalFloors - i + 1)"
           v-for="i in totalFloors"
           :key="i">
        F{{ totalFloors - i + 1 }}
      </div>
    </div>
    <div class="arrow down"
         @click="scrollDown"
         v-if="totalFloors > 5"></div>
    <div class="meta">共{{ totalFloors }}层</div>
  </div>
</template>

<script>
import {mapState} from 'vuex'
export default {
  methods: {
    floorChange (value) {
      this.$store.commit('patch_active_floor', value)
      this.$store.commit('patch_mode', 'floor')
    },
    getCurrentPage () {
      const total = this.totalFloors
      this.currentPage = Math.ceil(this.totalFloors / 5)
      this.marginTop = -(total - Math.min(this.currentPage * 5, total)) * 45
    },
    scrollDown () {
      if (this.currentPage === 1) {
        return
      }
      const total = this.totalFloors
      // 注意现在的toolbar为了实现重复点击收回楼层，存在设置curFloor的问题
      // 必须找时间重写toolbar的代码，解决这个奇异值的问题
      if (this.currentPage > Math.ceil(total / 5)) {
        this.currentPage = Math.ceil(total / 5)
      }
      this.currentPage -= 1
      this.marginTop =
        -(
          this.totalFloors -
          Math.min(this.currentPage * 5, this.totalFloors)
        ) * 45
    },
    scrollUp () {
      if (this.currentPage * 5 >= this.totalFloors) {
        return
      }
      this.currentPage += 1
      this.marginTop =
        -(
          this.totalFloors -
          Math.min(this.currentPage * 5, this.totalFloors)
        ) * 45
    }
  },
  mounted () {
    this.getCurrentPage()
  },
  props: {
    query: Object
  },
  data () {
    return {
      minFloor: 1,
      currentPage: 1,
      marginTop: 0
    }
  },
  computed: {
    ...mapState({
      totalFloors: state => state.buildingOverview.floors,
      activeFloor: state => state.activeFloor
    })
  }
}
</script>

<style lang="less" scoped>
.toolbar {
  position: absolute;
  right: 20px;
  bottom: 20px;
  user-select: none;
  text-align: center;
  pointer-events: initial;
  .arrow {
    width: 32px;
    height: 32px;
    cursor: pointer;
    margin: 0 auto;
    background-image: url(~@/assets/images/arrow_down.png);
    &.up {
      transform: rotate(180deg);
    }
  }
  .btn-group {
    max-height: 230px;
    overflow: hidden;
    .scroll-line {
      margin-top: -330px;
      transition: all 0.3s;
    }
  }
  .floor-btn {
    width: 90px;
    height: 40px;
    background-size: 100% 100%;
    background-image: url(~@/assets/images/floor_btn.png);
    cursor: pointer;
    color: #fff;
    line-height: 47px;
    font-size: 20px;
    font-family: PingFang;
    margin: 5px auto;
    &.active {
      background-image: url(~@/assets/images/floor_btn_light.png);
    }
  }
  .meta {
    color: #ccc;
    font-size: 16px;
    margin-top: 5px;
  }
}
</style>
